import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mahjong/const/MahjongSystemConst.dart';
import 'dart:math';
import 'package:mahjong/page/mahjong/ExportCard.dart';

class MahjongExportArea extends StatefulWidget {
  final List<Widget> exportCardList;
  final double width;
  final double height;
  final int scaleSize;
  MahjongExportArea(
      this.exportCardList, this.width, this.height, this.scaleSize);

  @override
  _MahjongExportAreaState createState() => _MahjongExportAreaState();
}

class _MahjongExportAreaState extends State<MahjongExportArea> {
  double _width, _height;
  List<Widget> _exportCardList;
  List<Widget> _pages;
  int _prvPageCount;
  PageController _pageController;
  int _scaleSize;
  int _currentPage;
  bool _isMove = false;
  @override
  void initState() {
    super.initState();
    _width = widget.width;
    _height = widget.height;
    _scaleSize = widget.scaleSize;
  }

  @override
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }

  void initData() {
    // 计算总页数
    int _pageCount = widget.exportCardList.length ~/ widget.scaleSize;
    _pageCount = widget.exportCardList.length % widget.scaleSize == 0
        ? _pageCount
        : _pageCount + 1;
    // 初始化 页面控制器
    if (_pageController == null) {
      _pageController =
          PageController(initialPage: _pageCount - 1, keepPage: true);
      _pageController.addListener(() {
        setState(() {
          _isMove = true;
          _currentPage = _pageController.page.round();
        });
      });
    }
    // 初始化 页面控制器显示的几个
    if (_currentPage == null) {
      _currentPage = _pageCount - 1;
    }
    int counter = 0;
    _pages = [];
    // 分页追加数据
    for (int j = 0; j < _pageCount; j++) {
      List<ExportCard> child = [];
      while (counter < (j + 1) * _scaleSize) {
        if (counter > widget.exportCardList.length - 1) {
          break;
        }
        child.add(widget.exportCardList[counter]);
        counter++;
      }
      _pages.add(Container(
        padding: EdgeInsets.only(
            top: MahjongSystemConst.exportAreaCardWidth *
                MahjongSystemConst.cardMasonryHScale /
                2),
        child: Wrap(spacing: 1.0, children: child),
      ));
    }
    // 当上一次的页数 被初始化了  如果别人抢了牌的话 可能会减牌
    if (_prvPageCount != null && _pageCount < _prvPageCount) {
      _pages.add(Container());
      _prvPageCount = _pageCount + 1;
    } else {
      _prvPageCount = _pageCount;
    }
    // 当 当前页数不是总页数大小   并且非移动  并且 是新增出牌的时候 需要 跳转页面
    if (_currentPage != _pageCount - 1 &&
        !_isMove &&
        _exportCardList.length < widget.exportCardList.length) {
      _currentPage = _pageCount - 1;
      _pageController.animateToPage(_currentPage,
          duration: new Duration(seconds: 1), curve: Curves.ease);
      //_pageController.jumpToPage(_currentPage);
    }
    _exportCardList = widget.exportCardList;
  }

  List<Container> buildTips(int count, int current) {
    List<Container> result = [];
    for (int j = 0; j < count; j++) {
      result.add(Container(
        decoration: BoxDecoration(
            color: j == current ? Colors.grey[300] : Colors.grey[500],
            borderRadius: BorderRadius.circular(8.0),
            boxShadow: [
              BoxShadow(
                  color: j == current ? Colors.grey[300] : Colors.grey[500],
                  offset: Offset(0, 0), //阴影xy轴偏移量
                  blurRadius: 1.0, //阴影模糊程度
                  spreadRadius: 1.0 //阴影扩散程度
                  )
            ]),
        height: 1.0,
        width: _width / 7,
      ));
    }
    return result;
  }

  @override
  Widget build(BuildContext context) {
    initData();
    _isMove = false;
    return _pages == null || _pages.length == 0
        ? Container()
        : Container(
            width: _width,
            height: _height,
            decoration: BoxDecoration(),
            clipBehavior: Clip.hardEdge,
            child: Stack(
              children: [
                _prvPageCount >= 2
                    ? Positioned(
                        top: MahjongSystemConst.exportAreaCardWidth *
                            MahjongSystemConst.cardMasonryHScale /
                            4,
                        child: Container(
                          width: _width,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: buildTips(_prvPageCount, _currentPage),
                          ),
                        ))
                    : Container(),
                PageView(
                  clipBehavior: Clip.none,
                  controller: _pageController,
                  children: _pages,
                ),
              ],
            ),
          );
  }
}
